package com.ljwm.security;

import com.ljwm.bootbase.security.LoginInfoHolder;
import com.ljwm.entity.User;
import com.ljwm.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * BootBaseDemo Created by yunqisong on 2018-05-22.
 * FOR:
 */
@Slf4j
@Service("userDetailsServiceImpl")
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        try {

            User user = userMapper.selectOne(new User().setUsername(s));

            LoginInfoHolder.setSalt(user.getSalt());

            return new JwtUser(user);
        } catch (Exception e) {
            log.info("Login Fail :\n {}", e);
        }
        return null;
    }
}
